package com.tiger.cloud.controller.crm;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.common.qiniu.MagicUploader;
import com.tiger.cloud.filter.IgnoreAuth;
import com.tiger.cloud.security.Base64;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("file")
@Api(value = "文件上传", tags = "文件")
public class FileController extends BaseController {

	@Autowired
	private MagicUploader magicUploader;

	@RequestMapping(value = "/uploadStaticFile", method = RequestMethod.POST)
	@ResponseBody
	@IgnoreAuth
	@ApiOperation(value = "apk单文件上传", notes = "apk单文件上传")
	public Response uploadStaticFile(@RequestParam MultipartFile file) throws IOException {
		if (file.isEmpty()) {
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID);
		} else {
			if(StringUtils.isEmpty( file.getOriginalFilename())){
				return ResponseUtil.error(ErrorCode.PARAMETER_INVALID);
			}
			FileUtils.copyInputStreamToFile(file.getInputStream(), new File("/opt/static/apps/android", file.getOriginalFilename()));
		}
		return ResponseUtil.ok();
	}

	@RequestMapping(value = "uploadToBase64", method = RequestMethod.POST)
	@ResponseBody
	@IgnoreAuth
	@ApiOperation(value = "Base64单文件上传", notes = "Base64单文件上传")
	public Response uploadToBase64(HttpServletRequest servletRequest, String imageFile, String filename) throws IOException {
		String imageFile_ = servletRequest.getParameter("imageFile");
		String filename_ = servletRequest.getParameter("filename");
		if (StringUtils.isEmpty(imageFile_)) {
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID, "imageFile必传");
		}
		if (StringUtils.isEmpty(filename)) {
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID, "filename必传");
		}
		byte[] bytes = Base64.decode(imageFile_); // 将字符串转换为byte数组
		String urlString = magicUploader.send(filename_, bytes);
		if (StringUtils.isEmpty(urlString)) {
			return ResponseUtil.error();
		}
		return ResponseUtil.ok(urlString);
	}

	@RequestMapping(value = "upload", method = RequestMethod.POST)
	@ResponseBody
	@IgnoreAuth
	@ApiOperation(value = "Form表单单文件上传", notes = "form表单单文件上传")
	public Response upload(MultipartFile imageFile) throws IOException {
		String urlString = magicUploader.send(imageFile.getOriginalFilename(), imageFile.getBytes());
		if (StringUtils.isEmpty(urlString)) {
			return ResponseUtil.error();
		}
		if (StringUtils.isEmpty(urlString)) {
			return ResponseUtil.error();
		}
		return ResponseUtil.ok(urlString);
	}

	@RequestMapping(value = "uploadMuti", method = RequestMethod.POST)
	@ResponseBody
	@IgnoreAuth
	@ApiOperation(value = "Form表单多文件上传", notes = "form表单多文件上传")
	public Response uploadMuti(MultipartHttpServletRequest iamgefile) throws IOException {
		Map<String, MultipartFile> map = iamgefile.getFileMap();
		Map<String, String> data = new HashMap<String, String>();
		for (String filename : map.keySet()) {
			MultipartFile multipartFile = map.get(filename);
			if (multipartFile != null) {
				String urlString = magicUploader.send(multipartFile.getOriginalFilename(), multipartFile.getBytes());
				data.put(multipartFile.getOriginalFilename(), urlString);
			}
		}
		return ResponseUtil.ok(data);
	}

}
